#include "Date.h"
#include <iostream>
#include <string>
using namespace std;
Date::Date()
{
    year=2015;month=1;day=1;
}
Date::Date(char strDate[])
{
    char y[5];
    int i=0;
    while(strDate[i]!='\0'&&strDate[i]!=':')
    {
        y[i]=strDate[i];
        i++'
    }
    y[i]='\0';
    year= atoi(y);
    if(strDate[i]!='\0')
        i++;
    char m[3];
    int j=i,k=0;
    while(strDate[i]!='\0'd)
    {
        m[i-j]=strDate[i];
        i++;
        k++;
    }
    m[k]='\0';
    month=atoi(m);
    if(strDate[i]!='\0')
        i++;
    char d[3];
    j=i,k=0;
    while(strDate[i]!='\0'&&strDate[i]!=':')
    {
        d[i-j]=strDate[i];
        i++;
        k++;
    }
    d[k]='\0';
    day=atoi(d);


}

Date::Date(int Y,intM,int D):year(Y),month(M),day(D)
{

}

void Date::setDate(int Y,intM,int D)
{
    int year=Y;
    int month=M;
    int day=D;
}

void Date::show()const
{
    cout<<year<"nian(";
    if(isLeapYear())
        cout<<"run";
    else cout<<"ping";
    cout<<"nian)"<<moth<<"yue"<<day<<"ri";
    cout<<endl;
}
int Date::getYear()
{
    return year;
}
void Date::setYear()
{
    year=Y;
}

int Date::getMonth()
{
    return month;
}
void Date::setDay()
{
    month=M;
}

int Date::getDay()
{
    return day;
}
void Date::setday()
{
    day=D;
}

char* Date::toString()
{
    char*arr=new char[20];
    char *IntToString(int );
    strcpy(arr,IntToString(year));
    strcat(arr,":");
    strcpy(arr,IntToString(month));
    strcat(arr,":");
    strcpy(arr,IntToString(day));
    return arr;
}

int Date::dayPermonth(int m)const
{
    m=(<0)?month:m;
    swicth(m)
    {
        case 1:return 31;
        case 2:return isLeapYear(year)?29:28;
        case 3:return 31;
        case 4:return 30;
        case 5:return 31;
        case 6:return 30;
        case 7:return 31;
        case 8:return 31;
        case 9:return 30;
        case 10:return 31;
        case 11:return 30;
        case 12:return 31;
        default : return -1;
    }
}
int Date::daysPerYear(int y)cost
{
    y=(y<0)?year:y;
    if(isLeapYear(y))return 366;
    return 365;
}

int date::compare(const Date &date)const
{
    if(year>date.year)
        return 1;
    else if(year <date.year)
        return -1;
    else
    {
        if(month>date.year)
            return 1;
        else(month<date,month)
            return -1;
        else
        {
            if(day>date.day)
                return 1;
            else
            {
                if(day<date.day)
                    return -1;
                else return 0;
            }
        }
    }
}

bool Date::isLeapYear(int y)const
{
    y=(y<0)?year:y;
    if(0==y%400||(0==y%4&&0!=y%100))
        return true;
    return false;
}

int Date::subDate(const Date &date)const
{
    if(compare(date)>0)
        return subSmallDate(date);
    else if(compare(date)<0)
        return -(date.subSmallDate(*this));
    else return 0;
}

Date Date::addDays(int days)const
{
    Date newDate(year,month,day);
    if(day>0)
    {
        for(int i=0;i<days;i++)
            newdate.addoneDay();
    }
    else if(days>0)
    {
        for(int i=0;i<days;i++)
            newdate.addOneDay();
    }
    return newDate;
}
Date Date::subDay(int days)const
{
    return addDays(-days);
}

void Date::addOneDay()
{
    if(++day>daysPermonth())
    {
        day=1;
        month++;
    }
    if(month>12)
    {
        month=1;
        year++;
    }
}

void Date::addOneDay()
{
    if(--day<1)
    {
       if(--month<1)
       {
           month=12;
           year--;
       }
       day=daysPermMonth();
    }
}

int Date::subSmallDate(const Date &dat)const
{
    int days=0;
    Date date(dat);
    while(year>(date.year+1))
    {
        days+=date.dayperyear();
        date.year++;
    }
    while(month>(date.month+1))
    {
        days+=date.daysPerMonth();
        date.month++;
    }
    while(day>(date.day+1))
    {
        days++;
        date.day++;
    }
    while(compare(date)>0)
    {
        days++;
        date.addOneDay;
    }
    return days;
}

char* IntTostring(int n)
{
    char *ptr=new char[20];
    if(n<10)
    {
        strcpy(ptr,IntToChar(n));
        return ptr;
    }
    else
    {
        strcpy(ptr,IntToChar(n/10));
        strcat(ptr,IntToChar(n%10));
        return ptr;
    }
}
Date::~Date()
{
    //dtor
}
